`路由`

import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
// 进度条，进入下一个页面时，在页面还没完全加载出来时使用
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
  showSpinner: false
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      // 二级路由
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        }
      ]
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      //  进入页面前判断路由
      beforeEnter(to) {
        if (to.query.payResult == 'false') return '/user/consult'
      }
    },
  ]
})
// 访问权限控制  导航守卫
router.beforeEach((to) => {
  // 加载进度条
  NProgress.start()
  console.log(to)
  // return true  是放行
  // 如果没有token且不在白名单里  直接到login页面
  const whiteList = ['/login', '/login/callback']
  // 状态存储仓库
  const store = useUserStore()
  if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
})
router.afterEach((to) => {
  // 修改标题
  document.title = `优医问诊-${to.meta.title || ''}`
  NProgress.done()
})

export default router
